<?php

// test1
/*
$error = '';
if(!isset($_POST['submitBtn'])) $file_exists = true;
else $file_exists = false;
*/

// test2
/*
$file_exists = false;
$error = 'Database error!';
*/

//test3
/*
$error = '';
if(!isset($_POST['submitBtn'])) $file_exists = true;
else {
  $file_exists = false;
  $error = 'Database error!';
}
*/

$error = '';
$file_exists = false;

if(isset($_POST['submitBtn']) && !unlink('./uml.db')) {
  $error = 'Can not create database file.';
}

if($error == '') {
  if(is_file('./uml.db') || is_link('./uml.db') || is_dir('./uml.db')) {
    $file_exists = true;
  }
  else {
    try {
      $sql = 'CREATE TABLE users (password TEXT, user TEXT UNIQUE)';
      $dbh = new PDO('sqlite:./uml.db', null, null);
      $r = $dbh->exec($sql);
      $dbh = null;
    }
    catch (Exception $e) {
      die("<p>Database error!</p>");
    }
  }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
 <title>User Management Lite</title>
 <link href="uml_style.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <div class="uml_main">
    <?php if($file_exists) {?>
      <div class="uml_caption">Warning</div>
      <div class="uml_result">The database file already exists. Would you like to delete it?</div>
      <form class="uml_form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="setupform">
        <input class="uml_submit" type="submit" name="submitBtn" value="DELETE">
      </form>
    <?php } elseif($error == '') {?>
      <div class="uml_caption">Setup result:</div>
      <div class="uml_result">Database setup completed successfully.</div>
    <?php } else {?>
      <div class="uml_caption">Setup result:</div>
      <div class="uml_result"><?php echo $error; ?><br/>Please fix the problem and <a href="<?php echo $_SERVER['PHP_SELF'] ?>">try again.</a></div>
    <?php } ?>
  </div>
</body>
</html>